#include "mainscene.h"
#include "config.h"
#include "QIcon"
#include "QTimer"
#include <QPainter>
#include <QMouseEvent>
mainScene::mainScene(QWidget *parent)
    : QWidget(parent)
{
    initScene();
}


mainScene::~mainScene()
{
}

void mainScene::initScene()
{
    //初始化窗口大小
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //窗口标题
    setWindowTitle(GAME_TITLE);
    //窗口标题
    setWindowIcon(QIcon(GAME_ICON));
    //设置定时器间隔
    m_Timer.setInterval(GAME_RATE);
    //启动游戏
    playGame();

}

void mainScene::playGame()
{
    //启动定时器
    m_Timer.start();
    //记录信号
    connect(&m_Timer , &QTimer::timeout,[=](){
        //更新元素坐标
        updatePosition();
        //游戏元素绘图
        update();

    });
}

void mainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();
}
void mainScene::paintEvent(QPaintEvent *event)
{
    //调用函数
    QPainter painter(this);
    QPainter paintershiled(this);
    //画出地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //画飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
}
void mainScene::mouseMoveEvent(QMouseEvent *event)
{
    int x= event->x() -m_hero.m_Rect.width()*0.5;
    int y= event->y() -m_hero.m_Rect.height()*0.5;
    //不出边界
    if(x <= 0 )
    {
        x = 0;
    }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
        x = GAME_WIDTH - m_hero.m_Rect.width();
    }
    if(y <= 0)
    {
        y = 0;
    }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
    {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}

